package com.example.occlusiondemo;

import android.widget.Toast;

import com.supermap.ar.areffect.ErrorCallback;
import com.supermap.ar.areffect.loader.ModelGroupScene;
import com.supermap.ar.areffect.AREffectElement;
import com.supermap.ar.areffect.TouchResult;

import java.util.ArrayList;

/**
 * 数据管理
 * <p>用于加载指定的管线场景</p>
 * <pre>SampleCode:
 * </pre>
 *
 * @author tanyx 2022/9/20
 * @version 1.0
 **/
public class DataManager {
    private volatile static DataManager dataManager = null;
    private ArrayList<ModelGroupScene> sceneLoaders;
    private ArrayList<AREffectElement> undergroundModelNodes = new ArrayList<>();
    private ArrayList<AREffectElement> wallModelNodes = new ArrayList<>();
    private ArrayList<AREffectElement> ceilingModelNodes = new ArrayList<>();
    private DataManagerCallback dataManagerCallback;
    public interface DataManagerCallback{
        void onClick(AREffectElement element , TouchResult touchResult);
    }

    private DataManager() {
        sceneLoaders = new ArrayList<>();
    }

    public void setDataManagerCallback(DataManagerCallback dataManagerCallback) {
        this.dataManagerCallback = dataManagerCallback;
    }

    public static DataManager getInstance(){
        if (dataManager == null){
            synchronized (DataManager.class){
                if (dataManager == null){
                    dataManager = new DataManager();
                }
            }
        }
        return dataManager;
    }

    public enum Type{
        UNDERGROUND,CEILING,WALL
    }
    public void addPipeScene(AREffectElement parent,String dataPath,boolean onClickEnabled,Type type){
        addPipeScene(parent,dataPath, onClickEnabled);
        switch (type){
            case WALL:
                wallModelNodes.add(parent);
                break;
            case CEILING:
                ceilingModelNodes.add(parent);
                break;
            case UNDERGROUND:
                undergroundModelNodes.add(parent);
                break;
        }
    }

    public void addPipeScene(AREffectElement parent,String dataPath,boolean onClickEnabled) {
        if (parent == null){
            Log.e("DataManager","addPipeScene failed! parent was null.");
            Toast.makeText(parent.getContext(), "未定位！", Toast.LENGTH_SHORT).show();
            return;
        }

        ModelGroupScene modelGroupScene = new ModelGroupScene(parent);
        modelGroupScene.setOnClickEnabled(onClickEnabled);
        modelGroupScene.setOnClickListener(new ModelGroupScene.OnClickListener() {
            @Override
            public void onClick(AREffectElement element, TouchResult touchResult) {
                com.eqgis.eqtool.tmp.VibrateHelper.vSimple(parent.getContext(),75);

                unSelectAll();
                element.select();
                if (dataManagerCallback != null){
                    dataManagerCallback.onClick(element, touchResult);
                }
            }
        });
        modelGroupScene.loadModelFolder(dataPath, "GLB", AREffectElement.VisualizerType.EMISSIVE_FACTOR, new ErrorCallback() {
            @Override
            public void onError(Error error) {
                if (error == null){
                    Log.i("DataManager-LoadData-successful",dataPath);
                }else {
                    Log.i("DataManager-LoadData-failed",dataPath);
                }
            }
        });
        sceneLoaders.add(modelGroupScene);
    }

    /**
     * 取消所有的选中状态渲染
     */
    public void unSelectAll(){
        for (int i = 0; i < sceneLoaders.size(); i++) {
            sceneLoaders.get(i).unSelectAll();
        }
    }

    public void setVisibleAll(boolean visible){
        setVisible(undergroundModelNodes,visible);
        setVisible(ceilingModelNodes,visible);
        setVisible(wallModelNodes,visible);
    }

    public void setVisibleInRollingFuncLayout(float yValue){
        if (yValue > 0.333f){
            setVisible(undergroundModelNodes,false);
            setVisible(ceilingModelNodes,true);
            setVisible(wallModelNodes,false);
        }else if (yValue < -0.333f){
            setVisible(undergroundModelNodes,true);
            setVisible(ceilingModelNodes,false);
            setVisible(wallModelNodes,false);
        }else {
            setVisible(undergroundModelNodes,false);
            setVisible(ceilingModelNodes,false);
            setVisible(wallModelNodes,true);
        }
    }

    private void setVisible(ArrayList<AREffectElement> modelNodes,boolean visible) {
        for (AREffectElement e: modelNodes) {
            e.setVisiblity(visible);
        }

    }
}
